
//音频播放
#include <SDL.h>
#include <SDL_audio.h>
#include <stdio.h>
typedef  struct
{
    SDL_AudioSpec spec;
    Uint8 *sound;               /* Pointer to wave data  音频数据指针*/
    Uint32 soundlen;            /* Length of wave data 音频数据大小*/
    int soundpos;               /* Current play position 播放进度*/
} Wave;
static Wave  wave;
static int done = 0;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
    SDL_Quit();
    exit(rc);
}

/**
 *
 * @param unused SDL_AudioSpec结构中的用户自定义数据，一般情况下可以不用。
 * @param stream 该指针指向需要填充的音频缓冲区
 * @param len 音频缓冲区的大小（以字节为单位）
 */
void SDLCALL fillerup(void *unused, Uint8 * stream, int len)
{
    Uint8 *waveptr;
    int waveleft;
    printf("callback len:%d, progress:%d\n",len, wave.soundpos * 100 /wave.soundlen);
    /* Set up the pointers */
    waveptr = wave.sound + wave.soundpos;
    //剩余待读取的数据 大小
    waveleft = wave.soundlen - wave.soundpos;

    /* Go! */
    if (waveleft <= len) { //剩余数据量小于缓冲区大小
        SDL_memcpy(stream, waveptr, waveleft);
        SDL_PauseAudio(1); //播放完成
        return;
    }
    SDL_memcpy(stream, waveptr, len);
    wave.soundpos += len; //调整进度

}
int main6(int argc, char *argv[])
{
    int i;
    char * fileName="audio/boom_start.wav";

    /* Enable standard application logging */
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

    /* Load the SDL library */
    //音频初始化
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
        return (1);
    }

    /* Load the wave file into memory 加载音频数据到内存*/
    if (SDL_LoadWAV(fileName, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
        quit(1);
    }

    wave.spec.callback = fillerup;//设置回调函数

    /* Show the list of available drivers */
    SDL_Log("Available audio drivers:");
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
        SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
    }

    /* Initialize fillerup() variables 打开音频设备 */
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
        SDL_FreeWAV(wave.sound);
        quit(2);
    }

    printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
    printf("SDL_AudioSpec.samples:%d\n",wave.spec.samples);
    printf("SDL_AudioSpec sample size:%d\n",wave.spec.format);//AUDIO_S16LSB
    printf("SDL_AudioSpec.channels:%d\n",wave.spec.channels);
    printf("SDL_AudioSpec.size:%d\n",wave.spec.size);//跟回调函数的len相等应该是样品数*样品所占字节数
    /* Let the audio run  开始播放*/
    SDL_PauseAudio(0);
    while ((SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))//获取音频状态， 只要在播放中，就一直循环，保证线程不退出
        SDL_Delay(1000);

    /* Clean up on signal */
    SDL_CloseAudio();//关掉音频进程以及音频设备
    SDL_FreeWAV(wave.sound);//释放数据由SDL_LoadWAV申请的
    SDL_Quit();
    printf("=========over==========\n");
    return (0);
}
